﻿CREATE proc [Notiz].[SaveTelefonNotiz]

@NotizID int,
@NameAnrufer nvarchar(50),
@Firma nvarchar(50) = null,
@TelefonNummer nvarchar(50),
@Betreff nvarchar(50),
@Notiz nvarchar(max) = null,
@NotizStrasse nvarchar(50),
@NotizPlz nchar(10),
@NotizOrt nvarchar(50),
@NotizEMail nvarchar(50) = null,
@Anrufzeit datetime,
@AnrufType int,
@TecUserID int,
@Status tinyint = 1,
@NotizOrgKey int = 0,
@Sex tinyint = 0,
@Users [dbo].[udtUsers] readonly
as
begin
	declare @RechtsFKey int,
			@rc int,
			@now datetime2(7) = sysdatetime(),
			@logText nvarchar(150)

	begin tran

	;with sel as
	(
		select	@NotizID		'NotizID',
				@NameAnrufer	'NameAnrufer',
				@Firma			'Firma',
				@TelefonNummer	'TelefonNummer',
				@Betreff		'Betreff',
				@Notiz			'Notiz',
				@NotizStrasse	'NotizStrasse',
				@NotizPlz			'Plz',
				@NotizOrt			'Ort',
				@NotizEMail		'NotizEMail',
				@NotizOrgKey	'NotizOrgKey',
				@Anrufzeit		'Anrufzeit',
				@AnrufType		'AnrufType',
				@TecUserID		'TecUserID',
				@Status			'Status',
				@Sex			'Sex'
	)
	merge into [Notiz].[datTelefonNotiz] as t
	using sel as s on
		s.NotizID = t.NotizID
	when not matched by target then
	insert (
			NotizOrgKey,
			NameAnrufer,
			Firma,
			TelefonNummer,
			Betreff,
			Notiz,
			Strasse,
			Plz,
			Ort,
			EMail,
			Anrufzeit,
			AnrufType,
			TecUser,
			[Status],
			[AnlageDatum],
			[Änderung],
			Sex
			)
	values (
			s.NotizOrgKey,
			s.NameAnrufer,
			s.Firma,
			s.TelefonNummer,
			s.Betreff,
			s.Notiz,
			s.NotizStrasse,
			s.Plz,
			s.Ort,
			s.NotizEMail,
			s.Anrufzeit,
			s.AnrufType,
			s.TecUserID,
			s.[Status],
			@now,
			@now,
			s.Sex
			)
	when matched then
	update set 
			NotizOrgKey = s.NotizOrgKey,
			NameAnrufer	= s.NameAnrufer,
			Firma		= s.Firma,
			TelefonNummer	= s.TelefonNummer,
			Betreff		= s.Betreff,
			Notiz		= s.Notiz,
			Strasse		= s.NotizStrasse,
			Plz			= s.Plz,
			Ort			= s.Ort,
			EMail		= s.NotizEMail,
			Anrufzeit	= s.Anrufzeit,
			AnrufType	= s.AnrufType,
			TecUser		= s.TecUserID,
			[Status]	= s.[Status],
			[Änderung]	= @now,
			Sex			= s.Sex
			;
		
	if coalesce(@NotizID,0) = 0
	begin
		set @NotizID = cast(@@IDENTITY as int)
	end

	delete	Notiz.mnUserNotiz
	where	NotizID = @NotizID

	if exists(select * from @Users)
	begin
		insert into Notiz.mnUserNotiz (UserID, NotizID)
		select	u.UserID, @NotizID
		from	@Users u
	end

	set @logText = 'Save NotizID: ' + cast(@NotizID as char(10))
	exec @rc = dbo.SaveLog
		@UserID = @TecUserID,
		@LogType = 'S',
		@LogArea = 'Notiz',
		@LogDetails = @logText,
		@IsError = 0
	
	if @rc != 0
	begin
		Select -1 'Return', 'dbo.SaveOrganisation has error' 'Error' 
		goto WithError
	end

	commit tran
	return 0

	
	
WithError:
	if @@TRANCOUNT > 0
	begin
		set @logText = 'Save NotizID: ' + cast(@NotizID as char(10))
		exec @rc = dbo.SaveLog
			@UserID = @TecUserID,
			@LogType = 'S',
			@LogArea = 'Notiz',
			@LogDetails = @logText,
			@IsError = 1
		rollback tran
	end
	return -1

end
GO


